﻿// ------------------------------------------------------------------------------------------------
//  <copyright file="Sites.cs" company="Iveely">
//    Copyright (c) Iveely Liu.  All rights reserved.
//  </copyright>
//  
//  <Create Time>
//    03/02/2013 21:59 
//  </Create Time>
//  
//  <contact owner>
//    liufanping@iveely.com 
//  </contact owner>
//  -----------------------------------------------------------------------------------------------

#region

using System;
using System.Diagnostics.CodeAnalysis;

#endregion

namespace IveelySE.Common.Html
{
    /// <summary>
    ///   站点目录
    /// </summary>
    [Serializable]
    public class Sites : IndexTree
    {
        /// <summary>
        ///   构造方法
        /// </summary>
        public Sites()
        {
            // 命名
            Root.Name = Function.Hash("sites");
        }

        /// <summary>
        ///   字符数组更改方法（将数组的最后一位去掉）
        /// </summary>
        /// <param name="str"> 输入的更改数组 </param>
        /// <returns> 返回去掉最后一位后的数组 </returns>
        private string[] strMu(string[] str)
        {
            var array = new SortedList<string>(str);
            array.RemoveAt(str.Length - 1);
            return array.ToArray();
        }

        /// <summary>
        ///   增加层次结构树
        /// </summary>
        /// <param name="parentUrl"> 父链接 </param>
        /// <param name="childUrl"> 子链接 </param>
        /// <param name="node"> 结点 </param>
        [SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1126:PrefixCallsCorrectly",
            Justification = "Reviewed. Suppression is OK here.")]
        private void Add(string[] parentUrl, string childUrl, Node node)
        {
            if(parentUrl.Length == 0)
            {
                var child = new Node
                                {
                                    Name = Function.Hash(childUrl),
                                    Parent = node
                                };
                node.Children.Add(child);
                return;
            }

            Node parent = Find(Function.Hash(parentUrl[parentUrl.Length - 1]), node);
            if(parent == null)
            {
                parent = new Node
                             {
                                 Name = Function.Hash(parentUrl[parentUrl.Length - 1]),
                                 Parent = node
                             };
                node.Children.Add(parent);
            }
            Add(strMu(parentUrl), childUrl, parent);
        }

        /// <summary>
        ///   向网站树中插入
        /// </summary>
        /// <param name="url"> 插入链接 </param>
        public void Insert(string url)
        {
            // 父链接
            string purl;

            // 如果以http://开头
            if(url.StartsWith("http://"))
            {
                // 替换掉
                purl = url.Replace("http://", string.Empty);
            }
            else if(url.StartsWith("https://")) // 如果是以https://开头
            {
                // 则替换掉
                purl = url.Replace("https://", string.Empty);
            }
            else
            {
                return;
            }

            // 执行添加
            this.Add(purl.Split('\\')[0].Split('.'), url, this.Root);
        }
    }
}